<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\SLessonInfo;
use App\Models\SLessonType;
use App\Models\STeacherInfo;
use App\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class LessonController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('课程')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('课程')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('课程')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('课程')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new SLessonInfo);

		$grid->id('Id')->sortable();
		$grid->users()->name('学生');
		$grid->teachers()->name('外教');
		// $grid->type_cloud_id('类型');
		$grid->column('类型')->display(function () {
			if ($this->cloud_id > 0) {
				return '云课堂';
			} else {
				return @$this->lessonTypes->name;
			}
		});
		$grid->begin_time('开始时间');
		$grid->end_time('结束时间');
		$grid->lesson_status('状态')->using(config('svs.lesson_status'));
		// $grid->lesson_cancle('是否取消');
		$grid->send_notice('短信')->using(config('svs.send_notice'));
		// $grid->evaluate_explain('Evaluate explain');
		// $grid->evaluate_vivid('Evaluate vivid');
		// $grid->evaluate_expand('Evaluate expand');
		// $grid->evaluate_advice('Evaluate advice');
		$grid->created_at('创建时间');
		// $grid->updated_at('Updated at');
		// $grid->deleted_at('Deleted at');
		$grid->actions(function ($actions) {
			$actions->disableDelete();
			$actions->disableEdit();
			// $actions->disableView();
		});
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(SLessonInfo::findOrFail($id));

		$show->panel()
			->tools(function ($tools) {
				// $tools->disableEdit();
				// $tools->disableList();
				$tools->disableDelete();
			});

		$show->id('Id');

		$show->user_id('学生')->as(function () {
			return $this->users->name;
		});
		$show->teacher_id('外教')->as(function () {
			return @$this->teachers->name;
		});

		$show->cloud_id('类型')->as(function () {
			if ($this->cloud_id) {
				return '云课堂';
			} else {

				return $this->lessonTypes->name;
			}
		});

		$show->begin_time('开课时间');
		$show->end_time('结束时间');
		$show->lesson_status('状态')->using(config('svs.lesson_status'));
		// $show->lesson_cancle('Lesson cancle');
		$show->send_notice('短信')->using(config('svs.send_notice'));
		$show->evaluate_explain('讲解清晰')->using(config('svs.evaluate_stars'));
		$show->evaluate_vivid('生动有效')->using(config('svs.evaluate_stars'));
		$show->evaluate_expand('拓展知识')->using(config('svs.evaluate_stars'));
		$show->evaluate_advice('建议信息');
		$show->created_at('创建时间');
		$show->updated_at('更新时间');
		// $show->deleted_at('Deleted at');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new SLessonInfo);
		$form->tools(function (Form\Tools $tools) {
			$tools->disableDelete();
		});

		// $users = User::pluck('name', 'id');
		// $form->select('user_id', '学生')->options($users);

		$form->select('user_id', '学生')->options(function ($id) {
			$user = User::find($id);

			if ($user) {
				return [$user->id => $user->name];
			}

		})->ajax('/ajax/users');

		// $teachers = STeacherInfo::pluck('name', 'id');
		$form->select('teacher_id', '老师')->options(function ($id) {
			$teacher = STeacherInfo::find($id);

			if ($teacher) {
				return [$teacher->id => $teacher->name];
			}

		})->ajax('/ajax/teachers');

		$types = SLessonType::pluck('name', 'id');
		$form->select('lesson_type_id', '课程类型')->options($types);

		$form->datetime('begin_time', '开始时间')->default(date('Y-m-d H:00:00'));
		$form->datetime('end_time', '结束时间')->default(date('Y-m-d H:30:00'));

		// $form->number('lesson_status', '状态');
		// $form->number('lesson_cancle', '课程取消');
		$form->select('send_notice', '发送短信')->options(config('svs.send_notice'))->default(0);
		// $form->number('evaluate_explain', 'Evaluate explain');
		// $form->number('evaluate_vivid', 'Evaluate vivid');
		// $form->number('evaluate_expand', 'Evaluate expand');
		// $form->text('evaluate_advice', 'Evaluate advice');
		$form->footer(function ($footer) {
			$footer->disableViewCheck();
			$footer->disableEditingCheck();
			$footer->disableCreatingCheck();
		});
		return $form;
	}
}
